package ioStream.file.processStream.byteStream;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.Serializable;

/**
 * Created with IntelliJ IDEA
 * Description:对象字节输出流学习
 * User:22954
 * Date:2024-03-18
 * Time:17:36
 */
@SuppressWarnings("all")
public class ObjectOutputStream_ {
    public static void main(String[] args) {
        //序列化后,保存文件的格式按JVM自定义的格式,后缀格式与实际格式无关,可以任意
        String filePath = "f:/Test/data.txt";
        ObjectOutputStream oos = null;
        try {
            oos = new ObjectOutputStream(new FileOutputStream(filePath));
            oos.writeInt(100);
            oos.writeChar('a');
            oos.writeBoolean(false);
            oos.writeFloat(5.5F);
            oos.writeUTF("hello");
            oos.writeObject(new Dog("Bob",5));
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                oos.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
class Dog implements Serializable {
    String name;
    int age;

    public Dog(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }
}
